#pragma once

#include<vector>
#include<functional>
namespace ForcibleBugMaker
{
    template <class T, class Container = std::vector<T>, class Compare = std::less<T> >
    class priority_queue
    {
    public:
        priority_queue() = default;

        void AdjustDown(size_t parent)
        {
            size_t child = 2 * parent + 1;
            while (child < _con.size()) {
                if (child + 1 < _con.size() && comp(_con[child], _con[child + 1]))++child;
                if (comp(_con[parent], _con[child])) {
                    std::swap(_con[parent], _con[child]);
                    parent = child;
                    child = 2 * parent + 1;
                }
                else break;
            }
        }

        void AdjustUp(size_t child)
        {
            size_t parent = (child - 1) / 2;
            while (child > 0) {
                if (comp(_con[parent], _con[child])) {
                    std::swap(_con[parent], _con[child]);
                    child = parent;
                    parent = (child - 1) / 2;
                }
                else break;
            }

        }

        template <class InputIterator>
        priority_queue(InputIterator first, InputIterator last)
        {
            while (first != last) {
                _con.push_back(*first);
                ++first;
            }
            for (int k = (_con.size() - 1 - 1) / 2; k >= 0; --k) {
                AdjustDown(k);
            }
        }

        bool empty() const
        {
            return _con.empty();
        }

        size_t size() const
        {
            return _con.size();
        }

        T& top() 
        {
            return _con[0];
        }

        void push(const T& x)
        {
            _con.push_back(x);
            AdjustUp(_con.size() - 1);
        }

        void pop()
        {
            std::swap(_con[0], _con[size() - 1]);
            _con.pop_back();
            AdjustDown(0);
        }
    private:
        Container _con;
        Compare comp;
    };
};